package com.example.common.component;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.example.common.R;

public class MyToast {
    private static final String TAG="MyToast";
    private static String text;
    protected static Toast toast=null;

    private static long oneTime=0;//第一次点击
    private static long twoTime=0;//第二次点击

    /**
     * 生成toast
     * @param context 当前上下文
     * @param message 显示文本内容
     * @param status 1表示通过，2表示警告，3表示错误，4表示系统提示
     */
    @SuppressLint("ResourceAsColor")
    public static void showMyToast(Context context, String message, int status){
        View view= LayoutInflater.from(context).inflate(R.layout.my_toast,null);
        GradientDrawable gradientDrawable=(GradientDrawable)view.getBackground();//获取shape.xml
        switch (status){
            case 1:
                gradientDrawable.setColor(ContextCompat.getColor(context,R.color.accept));//重新设置颜色
                break;
            case 2:
                gradientDrawable.setColor(ContextCompat.getColor(context,R.color.warn));//重新设置颜色
                break;
            case 3:
                gradientDrawable.setColor(ContextCompat.getColor(context,R.color.error));//重新设置颜色
                break;
            case 4:
                gradientDrawable.setColor(ContextCompat.getColor(context,R.color.system));
        }
        TextView textView=view.findViewById(R.id.common_my_toast_textView);

        if(toast==null){
            toast=new Toast(context);//创建toast实例
            toast.setView(view);//设置布局
            textView.setText(message);//消息
            toast.setDuration(Toast.LENGTH_SHORT);//时长
            toast.setGravity(Gravity.TOP|Gravity.CENTER_VERTICAL,0,50);//位置
            toast.show();
            Log.i(TAG, "showMyToast: 创建");
            oneTime=System.currentTimeMillis();
        }else {
            twoTime=System.currentTimeMillis();
            if(message.equals(text)){
                if(twoTime-oneTime>Toast.LENGTH_SHORT){
                    textView.setText(text);
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.TOP|Gravity.CENTER_VERTICAL,0,50);
                    toast.show();
                    Log.i(TAG, "showMyToast: 间隔过长后创建");
                }
            }else {
                text=message;
                toast.setView(view);
                textView.setText(message);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP|Gravity.CENTER_VERTICAL,0,50);
                toast.show();
                Log.i(TAG, "showMyToast: 显示");
            }
        }
    }

}
